package com.tourGuide.utils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class PasswordUtil {
    public static void main(String[] args) {
        //System.out.println(PasswordUtil.createPassword("1qaz@wsx"));
        System.out.println(PasswordUtil.readPassword("RRpcny4k+GdNVC79XJilOrTb/owQv+KlAhd4otLpoOoaUvlHlmnGjjpMnPexxpZHmQ36WI9mpuXJ8gr8vFsAMlDxbUOdW8eZQYAIEE8kRdxNsbdqCUvbvSSW80Nx7dIMmKmM5CggzkhsdHYJvdq6wcJqUdUpd9wSeVrpCRDhUlGE9ks7G40AiB89lpz+ZdvJvAfJaiSzQTdWIfY+gZBAw8aGNPFK6TTEnFher5rqgFTRj1kOWyJgMx8+bnWsqApes4dzWdklDLyNRdUnNKnwGiBFxbssFz9heSzLdTXranfPGohvpViMji5LFJ2tNG2tGCu7whyE7fkssZQ1FPiYlA=="));
    }

    private static final PublicKey PUBLIC_KEY = loadPublicKey();

    private static final PrivateKey PRIVATE_KEY = loadPrivateKey();

    private static PublicKey loadPublicKey() {
        try {
            byte[] buffer = Base64.getDecoder().decode("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv93xpl5/wPjTDBDdee09kvuqgcsVwEtZzvBKimzOqlhWUwSwc9hafFwbMM6dIWA/3pwxHfStYyPmp6LSe4huMj+A3lIpl4rzfwuSsf9zZhUqsy343Fv7ieM/UQlFQ/w/mydl/qEmz3nYROB/nF0LeL/qiQnW57OUiqFD4XQhAWiEPcYruHSzg6lig5hTgPEoqe9r+v5+lCgmHAQnlUYltPArASijW18OhMaDF1Kk5xWTXBLqi2rjj8p99HSmz9anHIdQhZUjLHOI5fZHJm0+UZrZT8ByiEmvTj8ADP37T821DtSJU18zcFMsIlM4LHEtufmxm5iRNNm+7sSX05DkFwIDAQAB".getBytes());
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            return keyFactory.generatePublic(keySpec);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException var3) {
            throw new RuntimeException("无此算法");
        }
    }

    private static PrivateKey loadPrivateKey() {
        try {
            byte[] buffer = Base64.getDecoder().decode("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC/3fGmXn/A+NMMEN157T2S+6qByxXAS1nO8EqKbM6qWFZTBLBz2Fp8XBswzp0hYD/enDEd9K1jI+anotJ7iG4yP4DeUimXivN/C5Kx/3NmFSqzLfjcW/uJ4z9RCUVD/D+bJ2X+oSbPedhE4H+cXQt4v+qJCdbns5SKoUPhdCEBaIQ9xiu4dLODqWKDmFOA8Sip72v6/n6UKCYcBCeVRiW08CsBKKNbXw6ExoMXUqTnFZNcEuqLauOPyn30dKbP1qcch1CFlSMsc4jl9kcmbT5RmtlPwHKISa9OPwAM/ftPzbUO1IlTXzNwUywiUzgscS25+bGbmJE02b7uxJfTkOQXAgMBAAECggEAbF2XeYbQExa2ZI81+AJlj5mywOIKI/SrAbU39Jzl0NJZ10PDwXpGCQ0gkl1v5ATvrfR7zVZ/g4ooYTAWP41b5+d0Cp1PPndNdcj8jqof7F0yI8doUlVlaYi/Hy7kPzS+XAxRkBJG0FIaMzS0/oSS0R1cAxBan70OvYm9xB2J8rexEX7/B+QpYggh0EDiQOgPMpHVsGyMV6CamlD21hstyLWYZuodOMCsfiXWN6yW1AkyYzS+7/Y739DVFlac07l1n6ESPT2CuYlbA6nQctgtLzzblm7zgGxbglG9RPxCTpt4x/bBuEHecGnBzFFM7dLKAcvgMeZHcbFz4/kkdft5YQKBgQDxNdEgZUBvVHIzzW/XBLtQt6wUeBe2PoLraSd9Mp9id4SVaUaS0pU7OtF06BQKcRyC6N1uvlkxFJlP4ILox+PMKZVN6d5jj4IaBClQpkNcgbumoZwdceCiHw10PESMVy5Yuzq8BMIljHfwwj/UtDz7YhOwpqb7KGqLWR6dkjfdhwKBgQDLoZrscgHYkX81PNoBb2SBf1oBEKVIGgWf4AUxZPRSG1zYaAbikhytv9xj2BF45IGOynRtGXMTxRX+UGSqj1KdB0Yf+NA8D7HZd1XMJhHpHpV5MCIszG/Ctr9nyotrJcIzi8+uQrMqzCdLPYdwc0LFJAkKdEXkJUHM0m1MB9Ho8QKBgBTwQAQY5fwED5nRlojKv1vlZcmtFxnZ2VGIIkZq84z/pQG3TsoF/7BRjcchMkQpx3jRsA8h9OoeyKQ3cAW2/F5Hqc9Gouhvl7Kcc92tYSGRnXF8i+m+beqE2Y0igizQkJRhRFr36saJqAu9QP882iJURDNMdChPPNh11wOEotbXAoGACdvgKVR3vGmG9CfnEiy3fAGTdPu+VNhVr2DkP3j4z0Td4OhgdPXIB62DWR4r8mN11lj8/A9AYBOpte9kgFGCGXIxtfiQ9Gd6H+JEOCcbtOKzL/Pho7OeMZoxeC/5Qg3I8j/xpSk7jXXm7SCkfxBFik/tVCE/BQW8wtLpUcYLJrECgYEAnaVjjFxwDQmdXUvzEZDLNARNcjD3SoDcljeEcksOWLY+ogEJmPkZicII5NZqc5l0KLv+9WuaRUM0KTNj9lgZhu4n25ugBM1iz66tSeIR35pK8aBAFVWGRL9K35fWKipOBLJBStjFVRmSS3aXx/9kweHEvhLb1ZEXG/hGvyxorrY=".getBytes());
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return keyFactory.generatePrivate(keySpec);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException var3) {
            throw new RuntimeException("无此算法");
        }
    }

    public static String readPassword(String password) {
        try {
            byte[] data = Base64.getDecoder().decode(password);
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(2, PRIVATE_KEY);
            return new String(cipher.doFinal(data));
        } catch (NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException var3) {
            throw new RuntimeException("读取密码失败", var3);
        }
    }

    public static String createPassword(String passwordSrc) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            byte[] data = passwordSrc.getBytes();
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(1, PUBLIC_KEY);
            int inputLen = data.length;

            int offSet = 0;

            for(int i = 0; inputLen - offSet > 0; offSet = i * 117) {
                byte[] cache;
                if (inputLen - offSet > 117) {
                    cache = cipher.doFinal(data, offSet, 117);
                } else {
                    cache = cipher.doFinal(data, offSet, inputLen - offSet);
                }

                out.write(cache, 0, cache.length);
                ++i;
            }

            byte[] encryptedData = out.toByteArray();
            return Base64.getEncoder().encodeToString(encryptedData);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException  var9) {
            throw new RuntimeException("创建密码失败", var9);
        }finally {
            if (null != out){
                try{
                    out.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }

}
